<?php
/**
 * @name Abstract of View
 *
 * LICENSE
 *
 * This source file is subject to the BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.phpxe.com/license/
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to Chaodong.Sheng <scd113@gmail.com> so we can send you a copy immediately.
 *
 * @category   View of Scd Framework
 * @package    Scd
 * @copyright  Copyright (c) 2007 PHPXe Web Studio <http://www.phpxe.com>
 * @license    http://framework.phpxe.com/license/
 * @version    $Id: Scd_View_Abstract.php Thu Sep 20 21:28:08 CST 2007 Davy $
 */
!defined("DS") ? define('DS', DIRECTORY_SEPARATOR) : '';

include_once("Scd/View/Exception.php");
class Scd_View_Abstract{

	var $_reg = null;

	var $_path = "www";//默认

	var $_file = null;

	function Scd_View_Abstract() {
	}

	function getInstance()
	{
		if (!is_object($this->_reg)) {
			$this->_reg = new Scd_View_Abstract();
		}
		return $this;
	}

	function set( $index, $value)
	{
		$this->$index = $value;
		$this->getInstance();
		$this->_reg->offsetSet($index, $value);
	}

	function get( $index )
	{
		if (!$this->_reg->offsetExists($index)) {
			scd::loadClass('scd.exception');
			return new Scd_Exception("No entry is registered for key '$index'");
		}
		return $this->_reg->offsetGet($index);
	}

	function setPath($path)
	{
		return $this->_path = $path;
	}

	function render( $name )
	{
		$this->_file = $this->_script($name);
		unset($name); // remove $name from local scope
		ob_start();
		$this->_run($this->_file);
		return ob_get_clean(); // need filter output
	}

	function escape( $name )
	{
		return nl2br($name);
	}
	
	function _script( $name )
	{
		return $this->_path. DS .$name;
	}

	/**
     * Clear all assigned variables
     *
     * @return void
     */
	function clearVars()
	{
		$vars   = get_object_vars($this);//返回由对象属性组成的关联数组
		foreach ($vars as $key => $value) {
			if ('_' != substr($key, 0, 1)) {
				unset($this->$key);
			}
		}
	}
}